package com.tzy.user.service;
import com.alibaba.fastjson.JSON;
import com.tzy.common.entity.WebContent;
import com.tzy.common.utils.HtmlParseUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class WebContentService {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    // 1、解析数据存入es
    public Boolean parseContent(String keywords) throws Exception {
// 解析查询出来的数据
        List<WebContent> contents = new HtmlParseUtil().parseJD(keywords);
// 封装数据到索引库中！
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(org.elasticsearch.common.unit.TimeValue.MINUS_ONE.timeValueMinutes(2));
        bulkRequest.timeout("2m");
        for(int i =0;i<contents.size();i++){
            bulkRequest
                    .add(new IndexRequest("jd_goods")
                            .source(JSON.toJSONString(contents.get(i)),XContentType.JSON));
        }
        BulkResponse bulkResponse =
                restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
        return !bulkResponse.hasFailures();
    }

//    /**
//     * 1、解析数据存入es
//     *
//     * @param keywords
//     * @return
//     * @throws Exception
//     */
//    public Boolean parseContent(String keywords) throws Exception {
//// 解析查询出来的数据
//        List<WebContent> contents = new HtmlParseUtil().parseJD(keywords);
//        log.info("contentsList="+contents);
//// 封装数据到索引库中！
//        BulkRequest bulkRequest = new BulkRequest();
////        bulkRequest.timeout(TimeValue.timeValueMinutes(2));
//        bulkRequest.timeout("2m");
//        for (int i = 0; i < contents.size(); i++) {
//            bulkRequest
//                    .add(new IndexRequest("jd_goods")
//                            .source(JSON.toJSONString(contents.get(i)), XContentType.JSON));
//            log.info("contents.get(i)="+contents.get(i));
//        }
//        BulkResponse bulkResponse =
//                restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//        return !bulkResponse.hasFailures();
//    }

//    /**
//     * 2、实现搜索功能，带分页处理
//     * @param keyword
//     * @param pageNo
//     * @param pageSize
//     * @return
//     * @throws IOException
//     */
//    public List<Map<String, Object>> searchContentPage(String keyword, int
//            pageNo, int pageSize) throws IOException {
//// 基本的参数判断！
//        if (pageNo <= 1) {
//            pageNo = 1;
//        }
//// 基本的条件搜索
//        SearchRequest searchRequest = new SearchRequest("jd_goods");
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//// 分页
//        sourceBuilder.from(pageNo);
//        sourceBuilder.size(pageSize);
//// 精准匹配 QueryBuilders 根据自己要求配置查询条件即可！
//        /**
//         * 3、controller
//         *  前端逻辑
//         *  1、定义导入vue和axios的依赖！
//         *  2、初始化Vue对象，给外层div绑定app对象！
//         */
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title",
//                keyword);
//        sourceBuilder.query(termQueryBuilder);
////        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//// 搜索
//        searchRequest.source(sourceBuilder);
//        SearchResponse response = restHighLevelClient.search(searchRequest,
//                RequestOptions.DEFAULT);
//// 解析结果！
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (SearchHit documentFields : response.getHits().getHits()) {
//            list.add(documentFields.getSourceAsMap());
//        }
//        return list;
//    }
}